package com.example.meeting.controller;

import com.example.meeting.model.domain.ToDo;
import com.example.meeting.model.responseData.CommonResult;
import com.example.meeting.service.IToDoService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Locale;

@Slf4j
@RestController
public class ToDoController {
    @Autowired
    private IToDoService todoService;

    @GetMapping("/todo")
    public CommonResult getUserToDoList(@RequestParam Integer id) {
        List<ToDo> todoList = todoService.getUserToDoList(id);
        return CommonResult.success(todoList);
    }

    @PostMapping("/todo")
    public CommonResult addTodo(@RequestBody ToDo todo) {

        /* SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date deadline = null;
        try {
            deadline = sdf.parse(todo.getDDL().toString());
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        todo.setDDL(deadline);*/

        todoService.addTodo(todo.getUser_id(), todo.getTodo(), todo.getFinish(), todo.getDDL());
        return CommonResult.success();
    }

    @GetMapping("/todoList")
    public CommonResult selectAdminList(
            @RequestParam Integer page,
            @RequestParam Integer pageSize,
            @RequestParam Integer userId // 通过URL参数接收用户ID
    ) {
        PageInfo<ToDo> todoList = todoService.selectTodoList(page, pageSize, userId);
        return CommonResult.success(todoList);
    }

    @DeleteMapping("/todo/{id}")
    public CommonResult deleteTodo(@PathVariable Integer id) {
        todoService.deleteTodo(id);
        return CommonResult.success();
    }

    @PutMapping("/todo")
    public CommonResult editTodo(@RequestBody ToDo todo) {
        todoService.editTodo(todo);
        return CommonResult.success();
    }

}
